package org.emmannuel.virtues.client.panel;

import java.util.ArrayList;

import org.emmannuel.virtues.client.RequesterAsync;
import org.emmannuel.virtues.client.common.TOSuggestion;
import org.emmannuel.virtues.client.common.TOVirtue;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;

public class AboutVirtuePanel extends VerticalPanel {

	private RequesterAsync greetingService;

	TOVirtue virtue = null;

	public AboutVirtuePanel(RequesterAsync greetingService) {
		//this.setVisible(false);
		this.greetingService = greetingService;
		add(virtueName);
		add(virtueDescription);
		HorizontalPanel hh = new HorizontalPanel();
		hh.addStyleName("main-panel");
		hh.add(suggLink);
		hh.add(commLink);
		add(hh);
		add(suggList);
		add(commList);
		add(adoptButton);
		adoptButton.addClickHandler(avch);
		addStyleName("virtue-detail");
	}

	HTML virtueName = new HTML();

	HTML virtueDescription = new HTML();
	
	HTML suggList = new HTML();
	
	HTML commList = new HTML();
	
	Hyperlink commLink = new Hyperlink("Comments", "");
	
	Hyperlink suggLink = new Hyperlink("Suggestions", "");

	Button adoptButton = new Button("Adopt");
	AdoptVirtueClickHandler avch = new AdoptVirtueClickHandler();
	
	private ArrayList<TOSuggestion> suggestionList = null;

	@SuppressWarnings("deprecation")
	public void showSelectedVirtue(TOVirtue virtue) {
		this.virtue = virtue;
		avch.setVirtue(virtue);
		suggLink.setTargetHistoryToken("Suggestions-"+virtue.getName());
		commLink.setTargetHistoryToken("Comments-"+virtue.getName());
		suggLink.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				askForSuggestions();
			}
		});
		
		commLink.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				askForCommets();
			}
		});

		virtueName.setHTML("Name: " + virtue.getName());
		virtueDescription
				.setHTML("Description <br/>" + virtue.getDescription());
		commList.setHTML("");
		suggList.setHTML("");
		
		
	}
	
	private void askForSuggestions(){
		suggList.setHTML("<br/>Processing...");
		commList.setHTML("");
		greetingService.getVirtueSuggestion(virtue.getId(), new AsyncCallback<ArrayList<TOSuggestion>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ArrayList<TOSuggestion> result) {
				suggestionList = result;
				processSuggestions();
				
			}
			
		});
	}
	private void processSuggestions() {
		
		StringBuilder str = new StringBuilder();
		suggList.setHTML("");
		if (suggestionList == null) {
			suggList.setHTML("No suggestions...");
		} else {
			str.append("<ul>");
			for (TOSuggestion item : suggestionList) {
				str.append("<li>" + item.getSuggestion() + "</li>");
			}
			str.append("</ul>");
			suggList.setHTML(str.toString());
		}
	}
	
	private void askForCommets(){
		commList.setHTML("No comments");
		suggList.setHTML("");
	}
	private void processComments() {
		commList.setHTML("");
		if (suggestionList == null) {

		} else {
			for (TOSuggestion item : suggestionList) {
				suggList.setHTML(suggList.getHTML() + "<br/>" + item.getSuggestion());
			}
		}
	}
}
